// pages/my/animate/index.js
var App = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: '',
		pre:[],
		idd:0,
		timelength:0,
		state:"开始",
		stat:"已完成",
        precent: [{
            color: "bg",
            time: "20"
        }, {
            color: "success",
            time: "40"
        }, {
            color: "warn",
            time: "40"
        }]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        var id = options.id;
		that.setData({
			idd:id
		})
        
    },
    enterSport: function(e) {
		var that = this;
		var idd = that.data.idd;
		var subid = e.currentTarget.dataset.subid;
		var totaltime = e.currentTarget.dataset.totaltime;
		var runTime = e.currentTarget.dataset.timelength;
		var timenos = e.currentTarget.dataset.timenodes;
		var timenodes = JSON.stringify(timenos)
		console.log(timenodes)
		if (totaltime == runTime){
			wx.showToast({
				title: '您已完成此次任务',
				icon:'none'
			})
		}
		else{
		console.log(runTime)
		console.log(totaltime)
         wx.navigateTo({
			 url: '../sport/index?subid=' + subid + '&totaltime=' + totaltime + '&idd=' + idd + '&runTime=' + runTime + '&timenodes=' + timenodes,
         })
		}
    },
    getList: function(id) {
		var that = this;
        App.ajax.ajax('schdule.do?method=runListBySchid', {
            JSESSIONID: wx.getStorageSync("JSESSIONID"),
            i_bb_schedule_id: id
        }, function(res) {
            console.log(res.data.result);
			var precents = [];
			for (var i = 0; i < res.data.result.length;i++){
				for (var j = 0; j < res.data.result[i].objsubs.length;j++){
					for (var k = 0; k < res.data.result[i].objsubs[j].jieduans.length;k++){
						//	precents.push(res.data.result[i].objsubs[j].jieduans[k].timeLength);
						res.data.result[i].objsubs[j].jieduans[k].second = res.data.result[i].objsubs[j].jieduans[k].timeLength
						res.data.result[i].objsubs[j].jieduans[k].timeLength = (res.data.result[i].objsubs[j].jieduans[k].timeLength / res.data.result[i].objsubs[j].totalTime *100).toFixed(2)	
					}
				}
			}
			console.log(res.data.result);
			var sum = 0;
			for(var p=0;p<precents.length;p++){
				sum += precents[p];
			}
			console.log(sum)
			var pre = [];
			for (var w = 0; w < precents.length;w++){
				pre.push(Math.ceil(((precents[w] / sum) * 100).toFixed(5)));
			}
			console.log(pre)
			that.setData({
				list: res.data.result,
				pre: pre
			})
        })
    },


	
    /**
	 * 
	 * 
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
		var that = this;
		that.getList(that.data.idd);
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})